﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using WpfApplication1.Models;
using System.Windows;
using System.Windows.Media.Imaging;
using WpfApplication1.Commands;
using Facebook;

namespace WpfApplication1.ViewModel
{
    class HomeFeedViewModel : INotifyPropertyChanged
    {
        static RootData rootdata = new RootData();
        static CFacebookServer fbServer = new CFacebookServer(MainWindow.AccessToken);

        StatusModel _status = null;
        PhotoModel _photo = null;
        LinkModel _link = null;

        static WindowDetail wndDetail;
        string _type;

        #region Status
        public Visibility Status_Visibility
        {
            get
            {
                if (_type == "status")
                    return Visibility.Visible;
                else
                    return Visibility.Collapsed;
            }
        }

        public BitmapImage Status_ProfilePicture
        {
            get
            {
                if (_status != null)
                {
                    BitmapImage bm = new BitmapImage();
                    bm.BeginInit();
                    string urlPic = string.Format("https://graph.facebook.com/{0}/picture?type={1}", _status.FromId, "square");
                    bm.UriSource = new Uri(urlPic);
                    bm.EndInit();
                    return bm;
                }
                else
                {
                    BitmapImage bm = new BitmapImage();
                    bm.BeginInit();
                    string urlPic = "http://profile.ak.fbcdn.net/static-ak/rsrc.php/v1/yL/r/HsTZSDw4avx.gif";
                    bm.UriSource = new Uri(urlPic);
                    bm.EndInit();
                    return bm;
                }
            }
        }

        public string Status_Header
        {
            get
            {
                if (_status != null)
                {
                    if (_status.Story == "")
                        return _status.FromName;
                    else
                        return _status.Story;
                }
                else
                {
                    return "";
                }
            }
        }

        public string Status_TimeUpdate
        {
            get
            {
                if (_status != null)
                {
                    DateTime time = DateTime.ParseExact(_status.UpdateTime,
                                     @"yyyy-MM-dd\THH:mm:ssK",
                                     null);
                    return time.ToString("hh:mm:ss tt dd-MM-yyyy");
                }
                else
                {
                    return "";
                }
            }
        }

        public string Status_Message
        {
            get {
                if (_status != null)
                    return _status.Message;
                else
                    return "";

            }
        }

        public string Status_SortMessage
        {
            get
            {
                if (_status != null)
                {
                    if (_status.Message != null)
                    {
                        if (_status.Message.Length > Constants.MAX_MESSAGE)
                        {
                            return _status.Message.Substring(0, Constants.MAX_MESSAGE) + " ...[Read More]";
                        }
                        else
                            return _status.Message;
                    }
                    else return "";
                }
                else
                {
                    return "";
                }
            }
        }

        public string Status_LikeCountText
        {
            get
            {
                if (_status != null)
                    return _status.LikeCountText;
                else
                    return "";
            }
            set
            {
                _status.LikeCountText = value;
                OnPropertyChanged("Status_LikeCountText");
            }
        }

        public string Status_CommentCountText
        {
            get
            {
                if (_status != null)
                    return _status.CommentCountText;
                else
                    return "";
            }
            set
            {
                _status.CommentCountText = value;
                OnPropertyChanged("Status_CommentCountText");
            }
        }
        #endregion

        #region Photo
        public Visibility Photo_Visibility
        {
            get
            {
                if (_type == "photo")
                    return Visibility.Visible;
                else
                    return Visibility.Collapsed;
            }
        }

        public BitmapImage Photo_ProfilePicture
        {
            get
            {
                if (_photo != null)
                {
                    BitmapImage bm = new BitmapImage();
                    bm.BeginInit();
                    string urlPic = string.Format("https://graph.facebook.com/{0}/picture?type={1}", _photo.FromId, "square");
                    bm.UriSource = new Uri(urlPic);
                    bm.EndInit();
                    return bm;
                }
                else
                {
                    BitmapImage bm = new BitmapImage();
                    bm.BeginInit();
                    string urlPic = "http://profile.ak.fbcdn.net/static-ak/rsrc.php/v1/yL/r/HsTZSDw4avx.gif";
                    bm.UriSource = new Uri(urlPic);
                    bm.EndInit();
                    return bm;
                }
            }
        }

        public string Photo_Header
        {
            get
            {
                if (_photo != null)
                {
                    if (_photo.Story == "")
                        return _photo.FromName;
                    else
                        return _photo.Story;
                }
                else
                {
                    return "";
                }
            }
        }

        public string Photo_TimeUpdate
        {
            get
            {
                if (_photo != null)
                {
                    DateTime time = DateTime.ParseExact(_photo.UpdateTime,
                                     @"yyyy-MM-dd\THH:mm:ssK",
                                     null);
                    return time.ToString("hh:mm:ss tt dd-MM-yyyy");
                }
                else
                {
                    return "";
                }
            }
        }

        public string Photo_SortMessage
        {
            get
            {
                if (_photo != null)
                {
                    if (_photo.Message != "")
                    {
                        if (_photo.Message.Length > Constants.MAX_MESSAGE)
                        {
                            return _photo.Message.Substring(0, Constants.MAX_MESSAGE) + " ...[Read More]";
                        }
                        else
                            return _photo.Message;
                    }
                    else return "";
                }
                else
                {
                    return "";
                }
            }
        }

        public string Photo_Message
        {
            get
            {
                if (_photo != null)
                {
                    if (_photo.Message != "")
                        return _photo.Message;
                    else
                        return "";
                }
                else
                {
                    return "";
                }
            }
        }

        public BitmapImage Photo_Picture
        {
            get
            {
                if (_photo != null)
                {
                    if (_photo.Picture != "")
                    {
                        BitmapImage bm = new BitmapImage();
                        bm.BeginInit();
                        bm.UriSource = new Uri(_photo.Picture);
                        bm.EndInit();
                        return bm;
                    }
                    else
                    {
                        BitmapImage bm = new BitmapImage();
                        bm.BeginInit();
                        bm.UriSource = new Uri("pack://application:,,,/Images/unknow_photo.png");
                        bm.EndInit();
                        return bm;
                    }
                }
                else
                {
                    BitmapImage bm = new BitmapImage();
                    bm.BeginInit();
                    bm.UriSource = new Uri("pack://application:,,,/Images/unknow_photo.png");
                    bm.EndInit();
                    return bm;
                }
            }
        }

        public string Photo_Title
        {
            get
            {
                if (_photo != null)
                {
                    if (_photo.Name == "")
                        return "";
                    else
                        return _photo.Name;
                }
                else
                {
                    return "";
                }
            }
        }

        public string Photo_LikeCountText
        {
            get {
                if (_photo != null)
                    return _photo.LikeCountText;
                
                else
                    return "";
            }
            set
            {
                _photo.LikeCountText = value;
                OnPropertyChanged("Photo_LikeCountText");
            }
        }

        public string Photo_CommentCountText
        {
            get {
                if (_photo != null)
                    return _photo.CommentCountText;
                else
                    return "";
            }
            set
            {
                _photo.CommentCountText = value;
                OnPropertyChanged("Photo_CommentCountText");
            }
        }

        #endregion

        #region Link

        public Visibility Link_Visibility
        {
            get
            {
                if (_type == "link")
                    return Visibility.Visible;
                else
                    return Visibility.Collapsed;
            }
        }

        public BitmapImage Link_ProfilePicture
        {
            get
            {
                if (_link != null)
                {
                    BitmapImage bm = new BitmapImage();
                    bm.BeginInit();
                    string urlPic = string.Format("https://graph.facebook.com/{0}/picture?type={1}", _link.FromId, "square");
                    bm.UriSource = new Uri(urlPic);
                    bm.EndInit();
                    return bm;
                }
                else
                {
                    BitmapImage bm = new BitmapImage();
                    bm.BeginInit();
                    string urlPic = "http://profile.ak.fbcdn.net/static-ak/rsrc.php/v1/yL/r/HsTZSDw4avx.gif";
                    bm.UriSource = new Uri(urlPic);
                    bm.EndInit();
                    return bm;
                }
            }
        }

        public string Link_Header
        {
            get
            {
                if (_link != null)
                {
                    if (_link.Story == "")
                        return _link.Name;
                    else
                        return _link.Story;
                }
                else
                    return "";
            }
        }

        public string Link_TimeUpdate
        {
            get
            {
                if (_link != null)
                {
                    DateTime time = DateTime.ParseExact(_link.UpdatedTime,
                                     @"yyyy-MM-dd\THH:mm:ssK",
                                     null);
                    return time.ToString("hh:mm:ss tt dd-MM-yyyy");
                }
                else
                    return "";
            }
        }

        public string Link_SortMessage
        {
            get
            {
                if (_link != null)
                {
                    if (_link.Message != "")
                    {
                        if (_link.Message.Length > Constants.MAX_MESSAGE)
                        {
                            return _link.Message.Substring(0, Constants.MAX_MESSAGE) + " ...[Read More]";
                        }
                        else
                            return _link.Message;
                    }
                    else return "";
                }
                else
                    return "";
            }
        }

        public string Link_Message
        {
            get
            {
                if (_link != null)
                {
                    return _link.Message;
                }
                else
                    return "";
            }
        }

        public string Link_PictureLink
        {
            get
            {
                if (_link != null)
                {
                    return _link.Picture;
                }
                else
                    return "";
            }
        }

        public BitmapImage Link_Picture
        {
            get
            {
                if (_link != null)
                {
                    if (_link.Picture != "")
                    {
                        BitmapImage bm = new BitmapImage();
                        bm.BeginInit();
                        bm.UriSource = new Uri(_link.Picture);
                        bm.EndInit();
                        return bm;
                    }
                    else
                    {
                        BitmapImage bm = new BitmapImage();
                        bm.BeginInit();
                        bm.UriSource = new Uri("pack://application:,,,/Images/unknow_photo.png");
                        bm.EndInit();
                        return bm;
                    }
                }
                else
                {
                    BitmapImage bm = new BitmapImage();
                    bm.BeginInit();
                    bm.UriSource = new Uri("pack://application:,,,/Images/unknow_photo.png");
                    bm.EndInit();
                    return bm;
                }
            }
        }

        public string Link_Title
        {
            get
            {
                if (_link != null)
                    return _link.Name;
                else
                    return "";
            }
        }

        public string Link_Caption
        {
            get
            {
                if (_link != null)
                    return _link.Caption;
                else
                    return "";
            }
        }

        public string Link_Description
        {
            get
            {
                if (_link != null)
                {
                    if (_link.Description.Length > Constants.MAX_MESSAGE)
                    {
                        return _link.Description.Substring(0, Constants.MAX_MESSAGE) + " ...[Read More]";
                    }
                    else
                        return _link.Description;
                }
                else
                    return "";
            }
        }

        public string Link_Link
        {
            get
            {
                if (_link != null)
                    return _link.Link;
                else
                    return "";
            }
        }

        public string Link_LikeCountText
        {
            get
            {
                if (_link != null)
                    return _link.LikeCountText;
                else
                    return "0";
            }
            set
            {
                _link.LikeCountText = value;
                OnPropertyChanged("Link_LikeCountText");
            }
        }

        public string Link_CommentCountText
        {
            get
            {
                if (_link != null)
                    return _link.CommentCountText;
                else
                    return "0";
            }
            set
            {
                _link.CommentCountText = value;
                OnPropertyChanged("Link_CommentCountText");
            }
        }

        #endregion

        #region Contructor

        public HomeFeedViewModel(string type, string id, IDictionary<string, object> from, string message,
            string story, IDictionary<string, object> storyTags, string createdTime, string updatedTime, string likeCountText, string commentCountText)
        {
            _type = type;
            if (type == "status")
                _status = new StatusModel(id, from, message, story, storyTags, createdTime, updatedTime, likeCountText, commentCountText);
        }

        public HomeFeedViewModel(string type, string id, IDictionary<string, object> from, string message, string story, string picture, string link, string name, string icon,
            string createdTime, string updatedTime, string likeCountText, string commentCountText)
        {
            _type = type;
            if (type == "photo")
                _photo = new PhotoModel(id, from, message, story, picture, link, name, icon, createdTime, updatedTime, likeCountText, commentCountText);
        }

        public HomeFeedViewModel(string type, string id, IDictionary<string, object> from, string message, string story, IDictionary<string, object> story_tags, string picture, string link,
            string name, string caption, string description, string created_time, string updated_time, string likeCountText, string commentsCountText)
        {
            _type = type;
            if (type == "link")
                _link = new LinkModel(id, from, message, story, story_tags, picture, link, name, caption, description, created_time, updated_time, likeCountText, commentsCountText);
        }

        #endregion

        public Command ClickCommand
        {
            get
            {
                return new Command(DoClick);
            }
        }

        public Command LikeCommand
        {
            get { return new Command(DoLike); }
        }

        void DoClick()
        {
            string id = "";
            if (_type == "status")
                id = _status.Id;
            else if (_type == "photo")
                id = _photo.Id;
            else
                id = _link.Id;

            if (wndDetail != null)
                wndDetail.Close();
            wndDetail = new WindowDetail(id, (string)rootdata.personalInfo["id"], (string)rootdata.personalInfo["name"], 100, 330, MainWindow.AccessToken);
            wndDetail.Show();
        }

        void DoLike()
        {
            string id = "";
            string textlike = "";
            if (_type == "status")
            {
                id = _status.Id;
                textlike = _status.LikeCountText;
            }
            else if (_type == "photo")
            {
                id = _photo.Id;
                textlike = _photo.LikeCountText;
            }
            else
            {
                id = _link.Id;
                textlike = _link.LikeCountText;
            }
            
            string result = fbServer.PostLike(id);
            if (result == null)
            {
                MessageBox.Show("Can not like this post!");
                return;
            }
            if (result.ToLower() == "true")
            {
                if (textlike == "0")
                    textlike = "You like this";
                else
                    textlike = "You and " + textlike + " other people(s) like this";
                if (_type == "status")
                    Status_LikeCountText = textlike;
                else if (_type == "photo")
                    Photo_LikeCountText = textlike;
                else
                    Link_LikeCountText = textlike;
            }
            else
            {
                MessageBox.Show("Can not like this post!");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
